package com.example.experiment07.controller;

import com.example.experiment07.entity.User;
import com.example.experiment07.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: YancyZhang
 * @time: 2022/6/21 12:31
 */
@RestController
@RequestMapping("/api/")
public class UserController {
    @GetMapping("index")
    public ResultVO getIndex() {
        return ResultVO.success(Map.of("name", "YancyZhang"));
    }

    @GetMapping("/users")
    public ResultVO getUserList(){
        List<User> userList = new ArrayList<>();
        User u1 = new User(1L,"yancy","12345");
        User u2 = new User(2L,"zhang","12345");
        userList.add(u1);
        userList.add(u2);
        return ResultVO.success(Map.of("userList", userList));
    }

    @GetMapping("/users/{uid}")
    public ResultVO getUserById(@PathVariable("uid") Long uid){
//        存在用户
        List<User> userList = new ArrayList<>();
        User u1 = new User(1L,"yancy","12345");
        User u2 = new User(2L,"zhang","12345");
        userList.add(u1);
        userList.add(u2);
        for(int i = 0; i < userList.size(); i++){
            if(userList.get(i).getId().equals(uid)){
                return ResultVO.success(Map.of("userInfo", userList.get(i)));
            }
        }
//        没有找到用户
        return ResultVO.error(400, "用户不存在！");
    }

    @PostMapping("/users")
    public ResultVO addUser(@RequestBody User user){
        System.out.println("模拟添加用户操作");
        return ResultVO.success(Map.of("userInfo", user));
    }

}